define(function(require, exports, module) {

	require("plugins/config/config.js");

	var $ = jQuery;
	var routers = [];

	$.fn.extend({

		route: function(options, params) {
			if(!options.router) {
				throw "缺少router属性！";
			}
			var opts = {
				beforeInit: function(vue) {},
				success: function(vue, isInitComplete) {},
				error:function(responseTxt, statusTxt, xhr) {}
			};
			$.extend(opts, options);
			var router = opts.router;
			if(routers.indexOf(router) < 0) {
				throw "路由不存在!";
			}
			var _this = $(this);
			var vue = null;
			_this.load($.toTplUrl("/" + router), null, function(responseTxt, statusTxt, xhr) {
				if(xhr.status == 200) {
					seajs.use(router, function(module) {
						var vueData = module();
						vueData.el = _this.get(0);
                        vue = new Vue(vueData);
                        vue.$nextTick(function() {
                        	var isInitComplete = true;
							opts.beforeInit(this);
							if(this.init) {
                            	if(this.init(params) == false) {
                            		isInitComplete = false;
                            	}
                            }
                            opts.success(this, isInitComplete);
                        });
					});
				} else {
					opts.error(responseTxt, statusTxt, xhr);
				}
			});
			return {

				destroy: function() {
					if(vue != null) {
						if(vue.destroy) {
							vue.destroy();
						}
						vue.$destroy();
					}
				}

			};
		}

	});

	module.exports = {

		addRouter: function(router) {
			if(this.hasRouter(router)) {
				return;
			}
			routers.push(router);
		},
		hasRouter: function(router) {
			return routers.indexOf(router) > -1;
		},
		addRouters: function(routers) {
			for(var i=0; i<routers.length; i++) {
				this.addRouter(routers[i]);
			}
		},
		removeRouter: function(router) {
			var index = routers.indexOf(router);
			if(index > -1) {
				arr.splice(index, 1);
			}
		},
		clearRouters: function() {
			routers = [];
		},
		route:function(selector, opts, params) {
			return $(selector).route(opts, params);
		}

	};

});